package com.hj.chartdemo.utils;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

public class SpaceItemDecoration extends RecyclerView.ItemDecoration {

  private int hSpace;
  private int vSpace;
  private int column;

  public SpaceItemDecoration(Context context, int hSpace, int vSpace, int column) {
    if (hSpace > 0) {
      this.hSpace = FunctionUtils.dip2px(context, hSpace);
    }
    if (vSpace > 0) {
      this.vSpace = FunctionUtils.dip2px(context, vSpace);
    }
    this.column = column;
  }

  @Override
  public void getItemOffsets(
      Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    // 不是第一个的格子都设一个左边和底部的间距
    outRect.left = hSpace;
    outRect.bottom = vSpace;
    // 由于每行都只有n个，所以第一个都是n的倍数，把左边距设为0
    if (parent.getChildLayoutPosition(view) % column == 0) {
      outRect.left = 0;
    }
  }
}
